<?php
declare(strict_types=1);

namespace App\Users;

use App\Controller;
use Field\SysApiStatRow;
use Field\SysUserConst;
use Helper\ValidateCode;

// hook controller_users_index_use.php

/**
 * @title   用户中心
 * @auth    true
 * @rank    10
 */
class Index extends Controller
{
    // hook controller_users_index_public_start.php

    // hook controller_users_index_public_end.php

    /**
     * @title   用户中心
     * @rank    99
     */
    public function action_Index()
    {
        return $this->View(get_defined_vars());
    }

    /**
     * @title   用户登录页
     * @rank    99
     */
    public function action_Login()
    {
        return $this->View(get_defined_vars());
    }

    /**
     * @title   用户注册页
     * @rank    99
     */
    public function action_Register()
    {
        return $this->View(get_defined_vars());
    }

    /**
     * @title  登录
     * @rank    99
     */
    public function action_Login_POST()
    {
        $vercode = $this->request->param('vercode');
        $code = $this->request->cookie(md5('xcode'));
        $username = $this->request->param('username');
        $password = $this->request->param('password');

        // hook zxadmin_index_login_post_start.php

    /*
        $_vc = new ValidateCode($this->_response);  //实例化一个对象
        if (!$_vc->verify($code, $vercode) || empty($code)) {
            $this->response('0003', [], '验证码错误');
        }
*/
        $user = $this->SysUser->read_by_username($username);
        if (empty($user->UserName) || md5($password . $user->Salt) !== $user->PassWord) {
            $this->response('0003', [], '账户或密码错误');
        }

        $user_token = $this->SysUser->make_token($this->request, $user);
        $this->setcookie(_CONF('session_id'), $user_token, 864000, true);
        $user->PassWord = '';
        $user->Salt = '';


        $userData = $user->toArray();
        $userData['url'] = '../index/index';
        $userData['token'] = $user_token;
        $userData['Avatar'] = $this->upload_domain . ($userData['AvatarAt'] ? '/' . $userData['UnID'] . '.png' : '/logo.png');
        unset($userData['PassWord'], $userData['Salt'], $userData['UserID'], $userData['ParentUserID']);
        $_vc = null;
        // hook zxadmin_index_login_post_start.php
        $this->response('0000', ['data' => $userData], '登陆成功');
    }

    /**
     * @title   退出登录
     * @rank    99
     */
    public function action_Logout()
    {
        $this->setcookie(_CONF('session_id'), '', -1);
        $this->response('0000', [], '', '/login', 302);
    }

    /**
     * @title   修改密码
     * @rank    99
     */
    public function action_Passwd_POST()
    {
        $old_password = $this->request->param('old_password');
        $new_password = $this->request->param('new_password');
        $this->CheckEmpty([$old_password, $new_password], ['旧密码', '新密码']);
        if (md5($old_password . $this->token->Salt) !== $this->token->PassWord) {
            $this->response('0003', [], '密码错误');
        }
        $salt = xn_rand(8);
        $this->SysUser->updateByPk($this->token->UserID, [
            SysUserConst::PassWord => md5($old_password . $salt),
            SysUserConst::Salt => $salt,
        ]);

        $this->setcookie(_CONF('session_id'), '', -1);
        $this->response('0000');
    }

    // hook controller_users_index_end.php
}
?>